package com.linuxgt.android.dbapp.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.linuxgt.android.dbapp.domain.User;

import java.util.ArrayList;
import java.util.List;

/**
 * Test表服务类
 * <p>
 * Created by liuteng on 2017/5/17.
 */

public class VIPUserService {

    private DBOpenHelper dbOpenHelper;
    private SQLiteDatabase sqLiteDatabaseW;
    private SQLiteDatabase sqLiteDatabaseR;

    /**
     * 构造器
     *
     * @param context
     */
    public VIPUserService(Context context) {
        dbOpenHelper = new DBOpenHelper(context);
        sqLiteDatabaseW = dbOpenHelper.getWritableDatabase();
        sqLiteDatabaseR = dbOpenHelper.getReadableDatabase();
    }

    /**
     * 保存记录
     *
     * @param user
     */
    public void save(User user) {
        ContentValues values = new ContentValues();
        values.put("name", user.getName());
        values.put("age", user.getAge());
        values.put("amount", user.getAmount());
        if (null != user.getId()) {
            values.put("id", user.getId());
        }
        sqLiteDatabaseW.insert("user", "name", values);
    }

    /**
     * 删除记录
     *
     * @param id
     */
    public void delete(Integer id) {
        sqLiteDatabaseW.delete("user", "id=?", new String[]{String.valueOf(id)});
    }

    /**
     * 按条件删除记录
     *
     * @param whereStr
     * @param values
     */
    public void delete(String whereStr, String[] values) {
        sqLiteDatabaseW.delete("user", whereStr, values);
    }

    /**
     * 根据id查询记录
     *
     * @param id
     * @return
     */
    public User query(Integer id) {
        Cursor cursor = sqLiteDatabaseR.query("user", null, "id=?", new String[]{String.valueOf(id)}, null, null, null);
        if (cursor.moveToFirst()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            int uid = cursor.getInt(cursor.getColumnIndex("id"));
            return new User(uid, name, age);
        }
        cursor.close();
        return null;
    }

    /**
     * 分页获取记录数
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public List<User> getPageData(int pageNum, int pageSize) {
        List<User> testList = new ArrayList<>();
        Cursor cursor = sqLiteDatabaseR.query("user", null, null, null, null, null, null, (pageNum - 1) * pageSize + "," + pageSize);
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            testList.add(new User(id, name, age));
        }
        cursor.close();
        return testList;
    }

    /**
     * 更新记录
     *
     * @param user
     */
    public void update(User user) {
        ContentValues values = new ContentValues();
        values.put("name", user.getName());
        values.put("age", user.getAge());
        sqLiteDatabaseW.update("user", values, "id=?", new String[]{String.valueOf(user.getId())});
    }

    /**
     * 获取总数
     *
     * @return
     */
    public long getCount() {
        Cursor cursor = sqLiteDatabaseR.query("user", new String[]{"id"}, null, null, null, null, null);
        cursor.moveToFirst();
        long count = cursor.getLong(0);
        cursor.close();
        return count;
    }
}
